﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Web;

namespace GodSoft.Code
{
    /// <summary>
    ///包括通用数据访问的类，业务逻辑层将会调用它
    /// </summary>
    public class GenericDataAccess
    {

        public static DataTable ExecuteSelectCommand(DbCommand command)
        {
            //将返回的datatable
            DataTable table;
            try
            {
                //打开数据连接
                command.Connection.Open();
                //执行该命令，将结果保存在DataTable
                DbDataReader reader = command.ExecuteReader();
                table = new DataTable();
                table.Load(reader);
                reader.Close();


            }
            catch (Exception ex)
            {
                Utilities.LogError(ex);
                throw;
            }
            finally
            {

                command.Connection.Close();
            }
            return table;
        }
        //*********创建并准备基于新连接的DbCommand对象(执行存储过程)
        public static DbCommand CreateCommand()
        {

            string dataProviderName = GodSoftConfiguration.DbProviderName;
            string connectionString = GodSoftConfiguration.DbConnectionString;
            //创建一个新的数据提供器工厂
            DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName);
            DbConnection conn = factory.CreateConnection();
            conn.ConnectionString = connectionString;
            DbCommand comm = conn.CreateCommand();
            comm.CommandType = CommandType.StoredProcedure;
            return comm;


        }
        //*********创建并准备基于新连接的DbCommand对象(执行Sql语句)
        public static DbCommand CreateCommandExeSql()
        {

            string dataProviderName = GodSoftConfiguration.DbProviderName;
            string connectionString = GodSoftConfiguration.DbConnectionString;
            //创建一个新的数据提供器工厂
            DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName);
            DbConnection conn = factory.CreateConnection();
            conn.ConnectionString = connectionString;
            DbCommand comm = conn.CreateCommand();
            comm.CommandType = CommandType.Text;
            return comm;


        }

        //执行update,insert,delete,返回受影响行数
        public static int ExecuteNonQuery(DbCommand command)
        {
            //受影响的行数
            int affectedRows = -1;
            //执行该命令以确保最后关闭数据库
            try
            {
                //打开该命令的数据库连接
                command.Connection.Open();
                //执行该命令并获取受影响的行数
                affectedRows = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //记录最终的错误并重新抛出它们
                Utilities.LogError(ex);
                throw;
            }
            finally
            {
                //关闭数据库
                command.Connection.Close();
            }
            return affectedRows;

        }

        //执行一个select命令并以字符串型是返回一个结果
        public static string ExecuteScalar(DbCommand command)
        {
            string value = "";
            //执行该命令并确保最后关闭数据库
            try
            {
                command.Connection.Open();
                value = command.ExecuteScalar().ToString();
            }
            catch (Exception ex)
            {
                Utilities.LogError(ex);
                throw;
            }
            finally
            {
                command.Connection.Close();
            }
            return value;

        }
    }
}